package com.fa.wisdom.ui;

import android.graphics.Bitmap;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.common.App;
import com.fa.wisdom.utils.TitleBuilder;
import com.google.zxing.WriterException;
import com.tuya.appsdk.sample.device.config.qrcode.QrCodeConfigActivity;
import com.tuya.appsdk.sample.device.config.util.qrcode.QRCodeUtil;
import com.tuya.appsdk.sample.resource.HomeModel;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.builder.ActivatorBuilder;
import com.tuya.smart.home.sdk.builder.TuyaCameraActivatorBuilder;
import com.tuya.smart.sdk.api.ITuyaActivator;
import com.tuya.smart.sdk.api.ITuyaActivatorGetToken;
import com.tuya.smart.sdk.api.ITuyaCameraDevActivator;
import com.tuya.smart.sdk.api.ITuyaSmartActivatorListener;
import com.tuya.smart.sdk.api.ITuyaSmartCameraActivatorListener;
import com.tuya.smart.sdk.bean.DeviceBean;
import com.tuya.smart.sdk.enums.ActivatorModelEnum;

import java.util.List;

public class QRConfigActivity extends BaseActivity {
    EditText et_ssid;
    EditText et_pwd;
    ImageView iv_qr;
    TextView tv_generate;
    LinearLayout ll_111;

    private ITuyaCameraDevActivator mTuyaActivator;
    @Override
    public void setListen() {
        et_ssid=findViewById(R.id.et_ssid);
        et_pwd=findViewById(R.id.et_pwd);
        iv_qr=findViewById(R.id.iv_qr);
        tv_generate=findViewById(R.id.tv_generate);
        ll_111=findViewById(R.id.ll_111);
        tv_generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ssid=et_ssid.getText().toString();
                String pwd=et_pwd.getText().toString();
                TuyaHomeSdk.getActivatorInstance().getActivatorToken(Constant.HOME_ID,
                        new ITuyaActivatorGetToken() {

                            @Override
                            public void onSuccess(String token) {
                                Log.e("ddddddd", "onSuccess: "+token);
                                ActivatorBuilder builder = new ActivatorBuilder()
                                        .setContext(App.getContext())
                                        .setSsid(ssid)
                                        .setPassword(pwd)
                                        .setActivatorModel(ActivatorModelEnum.TY_AP)
                                        .setTimeOut(100)
                                        .setToken(token)
                                        .setListener(new ITuyaSmartActivatorListener() {

                                                         @Override
                                                         public void onError(String errorCode, String errorMsg) {
                                                             Log.e("ddddddd", "onError: "+errorCode+"****"+errorMsg );
                                                         }

                                                         @Override
                                                         public void onActiveSuccess(DeviceBean devResp) {
                                                             Log.e("ddddddd", "onActiveSuccess: "+devResp );
                                                         }

                                                         @Override
                                                         public void onStep(String step, Object data) {
                                                             Log.e("ddddddd", "onActiveSuccess: "+step+"*****"+data );
                                                         }
                                                     }
                                        );
                                ITuyaActivator mTuyaActivator = TuyaHomeSdk.getActivatorInstance().newActivator(builder);
//开始配网
                                mTuyaActivator.start();
                            }

                            @Override
                            public void onFailure(String s, String s1) {

                            }
                        });






                // Get Network Configuration Token
//                TuyaHomeSdk.getActivatorInstance().getActivatorToken(Constant.HOME_ID,
//                        new ITuyaActivatorGetToken() {
//                            @Override
//                            public void onSuccess(String token) {
//                                Log.e("ddddd", "onSuccess: "+token );
//                                //Create and show qrCode
//                                TuyaCameraActivatorBuilder builder = new TuyaCameraActivatorBuilder()
//                                        .setToken(token)
//                                        .setPassword(ssid)
//                                        .setTimeOut(100)
//                                        .setContext(QRConfigActivity.this)
//                                        .setSsid(pwd)
//                                        .setListener(new ITuyaSmartCameraActivatorListener() {
//                                            @Override
//                                            public void onQRCodeSuccess(String qrcodeUrl) {
//                                                final Bitmap bitmap;
//                                                try {
//                                                    bitmap = QRCodeUtil.createQRCode(qrcodeUrl, 300);
//                                                    QRConfigActivity.this.runOnUiThread(new Runnable() {
//                                                        @Override
//                                                        public void run() {
//                                                            iv_qr.setImageBitmap(bitmap);
//                                                        }
//                                                    });
//                                                } catch (WriterException e) {
//                                                    e.printStackTrace();
//                                                }
//                                            }
//
//                                            @Override
//                                            public void onError(String errorCode, String errorMsg) {
//                                                Log.e("ddddd" ,"配置失败: "+errorCode+"****"+errorMsg);
//                                            }
//
//                                            @Override
//                                            public void onActiveSuccess(DeviceBean devResp) {
//                                                Log.e("ddddd" ,"配置成功: "+devResp);
//                                                Toast.makeText(QRConfigActivity.this,"config success!",Toast.LENGTH_LONG).show();
//                                            }
//                                        });
//                                mTuyaActivator = TuyaHomeSdk.getActivatorInstance().newCameraDevActivator(builder);
//                                mTuyaActivator.createQRCode();
//                                mTuyaActivator.start();
//                            }
//
//
//                            @Override
//                            public void onFailure(String errorCode, String errorMsg) {
//
//                            }
//                        });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mTuyaActivator) {
            mTuyaActivator.stop();
            mTuyaActivator.onDestroy();
        }
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("二维码配网")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_qr_confgi;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {

    }
}
